﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using Jumbot.Common;
using Jumbot.DataOper;
namespace JumbotCms.DataObject
{
    /// <summary>
    /// 会员表信息
    /// </summary>
    public class User
    {
        private string m_Id = "0";
        private int m_AdminId = 0;
        private string m_UserName = string.Empty;
        private string m_AdminName = string.Empty;
        private string m_UserPass = string.Empty;
        private string m_Question = string.Empty;
        private string m_Answer = string.Empty;
        private int m_Sex = 0;
        private string m_Email = string.Empty;
        private int m_Group = 0;
        private int m_State = 0;
        private int m_IsWriter = 0;
        private int m_IsVIP = 0;
        private string m_Cookies = string.Empty;
        private DateTime m_RegTime = DateTime.Now;
        private string m_VIPDate = DateTime.Now.AddDays(-1).ToShortDateString();
        private string m_RegIp = string.Empty;
        private DateTime m_LastTime = DateTime.Now;
        private string m_LastIp = string.Empty;
        private string m_HomePage = string.Empty;
        private string m_TrueName = string.Empty;
        private string m_IDType = string.Empty;
        private string m_IDCard = string.Empty;
        private string m_QQ = string.Empty;
        private string m_ICQ = string.Empty;
        private string m_MSN = string.Empty;
        private string m_BirthDay = string.Empty;
        private string m_ComeFrom = string.Empty;
        private string m_WorkUnit = string.Empty;
        private string m_Address = string.Empty;
        private string m_ZipCode = string.Empty;
        private int m_Login = 0;
        private int m_Points = 0;
        private int m_Integral = 0;
        private string m_Languages = string.Empty;
        private string m_GraduateFrom = string.Empty;
        private string m_StudyWay = string.Empty;
        private string m_MobileTel = string.Empty;
        private string m_Telephone = string.Empty;
        private string m_CardUserName = string.Empty;
        private string m_CardBank = string.Empty;
        private string m_CardNumber = string.Empty;

        public string Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }
        public int AdminId
        {
            get { return m_AdminId; }
            set { m_AdminId = value; }
        }
        public string UserName
        {
            set { m_UserName = value; }
            get { return m_UserName; }
        }
        public string AdminName
        {
            set { m_AdminName = value; }
            get { return m_AdminName; }
        }
        public string UserPass
        {
            set { m_UserPass = value; }
            get { return m_UserPass; }
        }
        public string Question
        {
            set { m_Question = value; }
            get { return m_Question; }
        }
        public string Answer
        {
            set { m_Answer = value; }
            get { return m_Answer; }
        }
        /// <summary>
        /// 性别：1为男，2为女，0为保密
        /// </summary>
        public int Sex
        {
            set { m_Sex = value; }
            get { return m_Sex; }
        }
        /// <summary>
        /// 邮件地址
        /// </summary>
        public string Email
        {
            set { m_Email = value; }
            get { return m_Email; }
        }
        /// <summary>
        /// 用户组
        /// </summary>
        public int Group
        {
            set { m_Group = value; }
            get { return m_Group; }
        }
        public int State
        {
            set { m_State = value; }
            get { return m_State; }
        }
        /// <summary>
        /// 是不是Writer
        /// </summary>
        public int IsWriter
        {
            set { m_IsWriter = value; }
            get { return m_IsWriter; }
        }
        /// <summary>
        /// 是不是VIP
        /// </summary>
        public int IsVIP
        {
            set { m_IsVIP = value; }
            get { return m_IsVIP; }
        }
        public string Cookies
        {
            set { m_Cookies = value; }
            get { return m_Cookies; }
        }
        public DateTime RegTime
        {
            set { m_RegTime = value; }
            get { return m_RegTime; }
        }
        public string VIPDate
        {
            set { m_VIPDate = value; }
            get { return m_VIPDate; }
        }
        public string RegIp
        {
            set { m_RegIp = value; }
            get { return m_RegIp; }
        }
        public DateTime LastTime
        {
            set { m_LastTime = value; }
            get { return m_LastTime; }
        }
        public string LastIp
        {
            set { m_LastIp = value; }
            get { return m_LastIp; }
        }
        public string HomePage
        {
            set { m_HomePage = value; }
            get { return m_HomePage; }
        }
        public string TrueName
        {
            set { m_TrueName = value; }
            get { return m_TrueName; }
        }
        /// <summary>
        /// 证件类型
        /// 1：身份证
        /// 2：军官证
        /// 3：学生证
        /// 4：其他证件
        /// </summary>
        public string IDType
        {
            set { m_IDType = value; }
            get { return m_IDType; }
        }
        /// <summary>
        /// 证件号码
        /// </summary>
        public string IDCard
        {
            set { m_IDCard = value; }
            get { return m_IDCard; }
        }
        public string QQ
        {
            set { m_QQ = value; }
            get { return m_QQ; }
        }
        public string ICQ
        {
            set { m_ICQ = value; }
            get { return m_ICQ; }
        }
        public string MSN
        {
            set { m_MSN = value; }
            get { return m_MSN; }
        }
        public string BirthDay
        {
            set { m_BirthDay = value; }
            get { return m_BirthDay; }
        }
        /// <summary>
        /// 所在地区
        /// </summary>
        public string ComeFrom
        {
            set { m_ComeFrom = value; }
            get { return m_ComeFrom; }
        }
        /// <summary>
        /// 工作单位
        /// </summary>
        public string WorkUnit
        {
            set { m_WorkUnit = value; }
            get { return m_WorkUnit; }
        }
        /// <summary>
        /// 联系地址
        /// </summary>
        public string Address
        {
            set { m_Address = value; }
            get { return m_Address; }
        }
        /// <summary>
        /// 邮政编码
        /// </summary>
        public string ZipCode
        {
            set { m_ZipCode = value; }
            get { return m_ZipCode; }
        }
        public int Login
        {
            set { m_Login = value; }
            get { return m_Login; }
        }
        /// <summary>
        /// 点数
        /// </summary>

        public int Points
        {
            set { m_Points = value; }
            get { return m_Points; }
        }
        /// <summary>
        /// 积分
        /// </summary>
        public int Integral
        {
            set { m_Integral = value; }
            get { return m_Integral; }
        }
        /// <summary>
        /// 擅长语种
        /// </summary>
        public string Languages
        {
            set { m_Languages = value; }
            get { return m_Languages; }
        }
        /// <summary>
        /// 毕业院校
        /// </summary>
        public string GraduateFrom
        {
            set { m_GraduateFrom = value; }
            get { return m_GraduateFrom; }
        }
        /// <summary>
        /// 研究方向
        /// </summary>
        public string StudyWay
        {
            set { m_StudyWay = value; }
            get { return m_StudyWay; }
        }
        /// <summary>
        /// 手机号码
        /// </summary>
        public string MobileTel
        {
            set { m_MobileTel = value; }
            get { return m_MobileTel; }
        }
        /// <summary>
        /// 联系电话
        /// </summary>
        public string Telephone
        {
            set { m_Telephone = value; }
            get { return m_Telephone; }
        }
        /// <summary>
        /// 开户名
        /// </summary>
        public string CardUserName
        {
            set { m_CardUserName = value; }
            get { return m_CardUserName; }
        }
        /// <summary>
        /// 开户行
        /// </summary>
        public string CardBank
        {
            set { m_CardBank = value; }
            get { return m_CardBank; }
        }
        /// <summary>
        /// 银行帐号
        /// </summary>
        public string CardNumber
        {
            set { m_CardNumber = value; }
            get { return m_CardNumber; }
        }

        public User()
        { }
        public void GetAllColumn(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT * FROM [jcms_normal_user] WHERE [Id]=" + _id;
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                Id = _id;
                AdminId = Validator.StrToInt(dr["AdminId"].ToString(), 0);
                UserName = dr["UserName"].ToString();
                AdminName = dr["AdminName"].ToString();
                UserPass = dr["UserPass"].ToString();
                Question = dr["Question"].ToString();
                Answer = dr["Answer"].ToString();
                Sex = Validator.StrToInt(dr["Sex"].ToString(), 0);
                Email = dr["Email"].ToString();
                Group = Validator.StrToInt(dr["Group"].ToString(), 0);
                State = Validator.StrToInt(dr["State"].ToString(), 0);
                IsWriter = Validator.StrToInt(dr["IsWriter"].ToString(), 0);
                Cookies = dr["Cookies"].ToString();
                RegTime = Validator.StrToDate(dr["RegTime"].ToString(), DateTime.Now);
                IsVIP = Validator.StrToInt(dr["IsVIP"].ToString(), 0);
                VIPDate = Validator.StrToDate(dr["VIPTime"].ToString(), DateTime.Now.AddDays(-1)).ToShortDateString();
                if (Jumbot.Common.Validator.ValidDate(VIPDate))//说明已经过期,再判断一次
                    IsVIP = 0;
                RegIp = dr["RegIp"].ToString();
                LastTime = Validator.StrToDate(dr["LastTime"].ToString(), DateTime.Now);
                LastIp = dr["LastIp"].ToString();
                HomePage = dr["HomePage"].ToString();
                TrueName = dr["TrueName"].ToString();
                IDType = dr["IDType"].ToString();
                IDCard = dr["IDCard"].ToString();
                QQ = dr["QQ"].ToString();
                ICQ = dr["ICQ"].ToString();
                MSN = dr["MSN"].ToString();
                BirthDay = dr["BirthDay"].ToString();
                ComeFrom = dr["ComeFrom"].ToString();
                WorkUnit = dr["WorkUnit"].ToString();
                Address = dr["Address"].ToString();
                ZipCode = dr["ZipCode"].ToString();
                Login = Validator.StrToInt(dr["Login"].ToString(), 0);
                Points = Validator.StrToInt(dr["Points"].ToString(), 0);
                Integral = Validator.StrToInt(dr["Integral"].ToString(), 0);
                Languages = dr["Languages"].ToString();
                GraduateFrom = dr["GraduateFrom"].ToString();
                StudyWay = dr["StudyWay"].ToString();
                MobileTel = dr["MobileTel"].ToString();
                Telephone = dr["Telephone"].ToString();
                CardUserName = dr["CardUserName"].ToString();
                CardBank = dr["CardBank"].ToString();
                CardNumber = dr["CardNumber"].ToString();
            }
        }
        public string GetUserName(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT [UserName] FROM [jcms_normal_user] WHERE [Id]=" + _id;
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0]["UserName"].ToString();
            }
            return string.Empty;
        }
        /// <summary>
        /// 实时判断会员是不是VIP
        /// </summary>
        /// <param name="_id"></param>
        /// <param name="doh"></param>
        /// <returns></returns>
        public bool VIP(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT IsVIP,VIPTime FROM [jcms_normal_user] WHERE [Id]=" + _id;
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                IsVIP = Validator.StrToInt(dr["IsVIP"].ToString(), 0);
                VIPDate = Validator.StrToDate(dr["VIPTime"].ToString(), DateTime.Now.AddDays(-1)).ToShortDateString();
                if (Jumbot.Common.Validator.ValidDate(VIPDate))//说明已经过期
                    IsVIP = 0;
            }
            return (IsVIP == 1);
        }
        /// <summary>
        /// 实时判断会员是不是Writer
        /// </summary>
        /// <param name="_id"></param>
        /// <param name="doh"></param>
        /// <returns></returns>
        public bool Writer(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT IsWriter FROM [jcms_normal_user] WHERE [Id]=" + _id;
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                IsWriter = Validator.StrToInt(dr["IsWriter"].ToString(), 0);
            }
            return (IsWriter == 1);
        }
        /// <summary>
        /// 升级为writer
        /// </summary>
        /// <param name="_id">用户ID</param>
        /// <param name="doh"></param>
        public void Change2Writer(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.ConditionExpress = "id=" + _id;
            doh.AddFieldItem("IsWriter", 1);
            doh.Update("jcms_normal_user");
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="_id">用户ID</param>
        /// <param name="_pass">修改后的密码</param>
        /// <param name="doh"></param>
        public void ChangePsd(string _id, string _pass, DbOperHandler doh)
        {
            doh.Reset();
            doh.ConditionExpress = "id=" + _id;
            doh.AddFieldItem("UserPass", _pass);
            doh.Update("jcms_normal_user");
        }
        /// <summary>
        /// 充值点数
        /// </summary>
        /// <param name="_id">用户ID</param>
        /// <param name="_points">加的点数</param>
        /// <param name="doh"></param>
        public void AddPoints(string _id, int _points, DbOperHandler doh)
        {
            doh.Reset();
            doh.ConditionExpress = "id=" + _id;
            doh.Add("jcms_normal_user", "Points", _points);
        }
        /// <summary>
        /// 扣除积分
        /// </summary>
        /// <param name="_id">用户ID</param>
        /// <param name="_integral">扣的积分</param>
        /// <param name="doh"></param>
        public void SubstractIntegral(string _id, int _integral, DbOperHandler doh)
        {
            doh.Reset();
            doh.ConditionExpress = "id=" + _id;
            doh.Substract("jcms_normal_user", "Integral", _integral);
        }
        /// <summary>
        /// 续费VIP
        /// </summary>
        /// <param name="_id">用户ID</param>
        /// <param name="_vipyears">续的年数</param>
        /// <param name="doh"></param>
        public void AddVIPYears(string _id, int _vipyears, DbOperHandler doh)
        {
            DateTime LimitDate = DateTime.Now;
            doh.Reset();
            doh.ConditionExpress = "id=" + _id;
            object[] values = doh.GetValues("jcms_normal_user", "IsVIP,VIPTime");
            bool _isvip = (Jumbot.Common.Validator.StrToInt(values[0].ToString(), 0) == 1);
            if (!_isvip)//如果还不是VIP
                LimitDate = DateTime.Now.AddYears(_vipyears);
            else
            {
                if (Jumbot.Common.Validator.ValidDate(values[1].ToString()))//如果已经过期
                    LimitDate = DateTime.Now.AddYears(_vipyears);
                else
                    LimitDate = DateTime.Parse(values[1].ToString()).AddYears(_vipyears);
            }
            doh.Reset();
            doh.ConditionExpress = "id=" + _id;
            doh.AddFieldItem("IsVIP", 1);
            doh.AddFieldItem("VIPTime", LimitDate);
            doh.Update("jcms_normal_user");
        }
    }
}
